package megrez.bussiness.schedule.web.rest;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
import megrez.bussiness.schedule.component.ScheduleManagerComponent;
import megrez.bussiness.schedule.entity.Job;
import megrez.bussiness.schedule.service.JobService;

/**
 * 定时任务接口
 * 
 * @author Lucky Yang
 * @since 0.0.1
 */
@RestController
@RequestMapping("/v1/schedules")
@RequiredArgsConstructor
public class ScheduleResource {
    private final ScheduleManagerComponent scheduleManagerComponent;
    private final JobService jobService;

    @GetMapping("/{jobId}/start")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void start(@PathVariable("jobId") Long jobId) {
        Job job = jobService.findJobById(jobId);
        scheduleManagerComponent.start(job);
    }

    @GetMapping("/{jobId}/stop")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void stop(@PathVariable("jobId") Long jobId) {
        Job job = jobService.findJobById(jobId);

        scheduleManagerComponent.shutdown(job);
    }

    @GetMapping("/shutdown-all")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void shutdownAll() {
        scheduleManagerComponent.shutdownAll();
    }

    @GetMapping("/start-all")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void startAll() {
        scheduleManagerComponent.startAll();
    }
}
